package state.component;


import state.DrawListener;
import state.GameConstants;
import state.Graphics;
import state.KeySpriteListener;
import state.Manager;
import state.Sprite;
import state.TouchScreenListener;
import state.font.FontManager;
import state.font.ImageText;
import state.util.Color;
import state.util.ImageUtil;

public abstract class Component extends Sprite implements DrawListener, KeySpriteListener, TouchScreenListener {
	protected boolean isFillBackGround = true;
	protected int backgroundColor = Color.WHITE_CODE;
	
	protected boolean isEnable = true;
	protected int forceGroundColor = Color.BLACK_CODE;
	
	protected boolean hasBorder = true;
	protected int borderColor = Color.RED2_CODE;
	
	protected boolean isFocused = false;
	protected boolean focusable = true;
	protected int focusBgColor = Color.GREEN_CODE; 
	protected int focusFgColor = Color.RED2_CODE;
	
	private ImageText font;
	
	public Component() {
		super(0, 0, GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT);
		init();
	}
	
	/**
	 * Constructor
	 * 
	 * @param x - Tọa độ x của Component
	 * @param y - Tọa độ y của Component
	 * @param width - Chiều rộng của Component
	 * @param height - Chiều dài của Component
	 */
	public Component(int x, int y, int width, int height) {
		super(x, y, width, height);
		init();
	}
	
	/**
	 * Constructor
	 * 
	 * @param manager - Manager dùng để quản lý việc hiển thị
	 * @param width - Chiều rộng của Component
	 * @param height - Chiều dài của Component
	 */
	public Component(Manager manager, int width, int height) {
		super(manager, width, height);
		init();
	}

	/**
	 * Constructor
	 * 
	 * @param manager - Manager dùng để quản lý việc hiển thị
	 * @param x - Tọa độ x của Component
	 * @param y - Tọa độ y của Component
	 * @param width - Chiều rộng của Component
	 * @param height - Chiều dài của Component
	 */
	public Component(Manager manager, int x, int y, int width, int height) {
		super(manager, x, y, width, height);
		init();
	}
	
	protected void init() {
		setDrawListener(this);
		setTouchScreenListener(this);
		setKeyListener(this);
	}
	
	public abstract void draw(Graphics g);
	
	public void paint(Sprite source, Graphics g) {
		draw(g);
	}
	
	public void setManager(Manager manager) {
	  if (manager != null) {
      manager.append(this);
    }
	}
	
	public Component setStyle(Style style) {
		forceGroundColor = style.forceGroundColor;
		backgroundColor = style.backgroundColor;
		isEnable = style.isEnable;
		hasBorder = style.hasBorder;
		borderColor = style.borderColor;
		isFocused = style.isFocused;
		focusable = style.focusable;
		isFillBackGround = style.isFillBackGround;
		focusBgColor = style.focusBgColor;
		focusFgColor = style.focusFgColor;
		borderColor = style.borderColor;
		setFont(style.font);
		if (style.backgroundImageName == null) {
			setImage(null, false);
		} else {
			setImage(ImageUtil.getImage(style.backgroundImageName), false);
		}
		return this;
	}

	public ImageText getFont() {
		if (font == null) {
			font = FontManager.getFont(FontManager.FONT_SIZE_8);
		}
		return font;
	}

	public Component setFont(ImageText font) {
	  if (font == null) {
	    return this;
	  }
	  
		this.font = font;
		return this;
	}

	/** Focus back ground color */
	public int getFocusBgColor() {
		return focusBgColor;
	}

	/** Focus back ground color */
	public Component setFocusBgColor(int focusBgColor) {
	  isFillBackGround = true;
	  setFocusable(true);
		this.focusBgColor = focusBgColor;
		return this;
	}

	/** Focus force ground color */
	public int getFocusFgColor() {
		return focusFgColor;
	}

	/** Focus force ground color */
	public Component setFocusFgColor(int focusFgColor) {
		this.focusFgColor = focusFgColor;
		setFocusable(true);
		return this;
	}

	public int getBackgroundColor() {
		return backgroundColor;
	}

	public Component setBackgroundColor(int backgroundColor) {
		isFillBackGround = true;
		this.backgroundColor = backgroundColor;
		return this;
	}
	
	public boolean isFillBackGround() {
		return isFillBackGround;
	}

	public Component setFillBackGround(boolean isFillBackGround) {
		this.isFillBackGround = isFillBackGround;
		return this;
	}

	public int getForceGroundColor() {
		return forceGroundColor;
	}

	public Component setForceGroundColor(int forceGroundColor) {
		this.forceGroundColor = forceGroundColor;
		return this;
	}

	public boolean isEnable() {
		return isEnable;
	}

	public Component setEnable(boolean isEnable) {
		this.isEnable = isEnable;
		return this;
	}

	public boolean isHasBorder() {
		return hasBorder;
	}

	public Component setHasBorder(boolean hasBorder) {
		this.hasBorder = hasBorder;
		return this;
	}

	public int getBorderColor() {
		return borderColor;
	}

	public Component setBorderColor(int borderColor) {
		hasBorder = true;
		this.borderColor = borderColor;
		return this;
	}

	public boolean isFocused() {
		return isFocused;
	}

	public Component setFocused(boolean isFocused) {
		if (focusable) {
			this.isFocused = isFocused;
		}
		return this;
	}

	public boolean isFocusable() {
		return focusable;
	}

	public Component setFocusable(boolean focusable) {
		this.focusable = focusable;
		if (!focusable) {
			isFocused = false;
		}
		return this;
	}
	
	public void keyPressed(Sprite source, int keyCode) {
  }

  public void keyReleased(Sprite source, int keyCode) {
  }

  public void keyRepeated(Sprite source, int keyCode) {
  }

  public void pointerDragged(Sprite source, int x, int y) {
  }

  public void pointerPressed(Sprite source, int x, int y) {
  }

  public void pointerReleased(Sprite source, int x, int y) {
  }

  public Sprite detroy() {
		super.detroy();
		font = null;
		return this;
	}
}
